# This code is hosted on http://code.google.com/p/lenthorp/
# Freely available for use in applications, but should NOT be modified
# Email all comments to lenthorpresearch@gmail.com

import sys, os
mainLocation = os.path.dirname(sys.path[0]) + '\\'
directories = []
directories.append(mainLocation + 'Auxiliary')
for directory in directories:
        if not directory in sys.path:
            sys.path.append(directory)
import Settings
mainLocation = Settings.mainLocation
MySQLUsername = Settings.MySQLUsername

def BuildDatabase(name, location):
    import MySQLScript
    MySQLScript.ExecuteMySQL(mainLocation + location + '\\' + name, MySQLUsername)


def initialiseSession():
    
    import SessionInitialise_NoDB 
    SessionInitialise_NoDB.initialiseSession()

    print('... Building marketData database ...')
    location = 'DataHandling' + '\\' + 'MySQLLoading'
    built = False
    try:
        BuildDatabase('marketData.sql', location)
        built = True
    except:
        pass
    if not built:
        print('... ... Trying again to build marketData database ...')
        try:
            BuildDatabase('marketData.sql', location)
            built = True
        except:
            print('... ... Failed to build marketData database ...')

    print('... Building historicalDly database ...')
    location = 'DataHandling' + '\\' + 'MySQLLoading'
    built = False
    try:
        BuildDatabase('historicalDly.sql', location)
        built = True
    except:
        pass
    if not built:
        print('... ... Trying again to build historicalDly database ...')
        try:
            BuildDatabase('historicalDly.sql', location)
            built = True
        except:
            print('... ... Failed to build historicalDly database ...')


    print('... Building regressionResults database ...')
    location = 'RegressionTesting'
    built = False
    try:
        BuildDatabase('regressionResults.sql', location)
        built = True
    except:
        pass
    if not built:
        print('... ... Trying again to build regressionResults database ...')
        try:
            BuildDatabase('regressionResults.sql', location)
            built = True
        except:
            print('... ... Failed to build regressionResults database ...')

    print('... Compiling main QuantLib SWIG file ...')
    import SWIGCompile
    SWIGCompile.run(mainLocation)

from imp import reload

if __name__ == "__main__":
    initialiseSession()